/*
 * 
 */
package es.drOsu.game;

import java.util.List;

import es.drOsu.common.Tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class ShakeBar clase de pruebas para el bonus de shakebar(agitar el telefono y rellenar la barra).
 */
public class ShakeBar extends Activity implements SensorEventListener{
	
	/** The TAG variable para la depuracion y el LogCat. */
	private final String TAG = "ShakeBar";
	
	/** The m sensor manager guarda el manejador de sensores del dispostivo. */
	private SensorManager mSensorManager;
	
	/** The sensor list listta con todos los sesores de los que dispone el dispositivo. */
	private List<Sensor> sensorList;
	
	/** The shakebar view vista en la que se pintara todo el bonus shakebar. */
	private View shakebarView;
	//private Drawable bar;
	/** The m accelerometer values guarda los valores del acelerometro. */
	private float[] mAccelerometerValues={0,0,0};
	
	/** The start x coordenada x donde empezara a pintar la barra. */
	private int startX = 0;
	
	/** The start y coordenada y donde empezara a pintar la barra. */
	private int startY = 0;
	
	/** The stop x coordenada x donde finalizara a pintar la barra. */
	private int stopX = 0;
	
	/** The stop y coordenada y donde finalizara a pintar la barra. */
	private int stopY = 0;
	
	/** The firsttime variable de control para el primer pintado del bonus. */
	private boolean firsttime = true;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    shakebarView = new ShakeBarView(this);
		setContentView(shakebarView);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		//compass = getResources().getDrawable(R.drawable.compass);
	}

		

	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume(){
		super.onResume();
    	sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    	for (Sensor sensor : sensorList)
    		mSensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
	protected void onPause(){
    	super.onPause();
    	//Stop updates to save power while app paused
    	mSensorManager.unregisterListener(this);
    }
    
    /**
     * The Class ShakeBarView vista donde se pintaran todos los elementos del bonus shake bar.
     */
    public class ShakeBarView extends View{


		/** The background_bar imagen de fondo del bonus. */
		private Drawable background_bar = null;
		
		/** The pink_bar relleno de fondo de la barra . */
		private Drawable pink_bar = null;
		
		/** The m context contexto de la activdad. */
		private Context mContext;


		/**
		 * Instantiates a new shake bar view inicializacion de la vista sin parametros extra.
		 *
		 * @param context the context
		 */
		public ShakeBarView(Context context) {
    		super(context);
    		mContext = context;
    		this.setBackgroundResource(R.drawable.shake_bar_wallpaper);
    	}
		

    	/* (non-Javadoc)
	     * @see android.view.View#onDraw(android.graphics.Canvas)
	     */
	    @Override
    	protected void onDraw(Canvas canvas) {
    		//Drawable background = Tools.resizeImage(BitmapFactory.decodeResource(this.getResources(),R.drawable.shake_bar_wallpaper), getHeight(), getHeight());
    		//background.draw(canvas);
    		startX = (int) (shakebarView.getWidth()/2 - (25*mContext.getResources().getDisplayMetrics().density));
    		stopX = (int) (startX+50*mContext.getResources().getDisplayMetrics().density);
    		startY = shakebarView.getHeight()*3/4;
    		Paint green = new Paint();
    		green.setColor(getResources().getColor(R.color.drak_pink_bar));
    		if (firsttime == true){
    			
    			stopY = shakebarView.getHeight()*1/4;
    			background_bar = Tools.resizeImage(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.background_bar), stopX-startX,startY-stopY);
    			pink_bar = Tools.resizeImage(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.pink_bar), stopX-startX,startY-stopY);
    			stopY=startY-1;
    			firsttime = false;
    		}
    		
    		
    		//Paint green = new Paint();
    		//green.setColor(getResources().getColor(R.color.green));
    		//Log.d("BAR",""+ (int) Math.abs(mAccelerometerValues[1]));
    		stopY -=(int) Math.abs(mAccelerometerValues[1]);
    		//Log.d("BAR", "stopY antes if: "+stopY+"parada: "+shakebarView.getHeight()*1/4);
    		if(stopY > (int)((startY-background_bar.getIntrinsicHeight())/mContext.getResources().getDisplayMetrics().density)){//(shakebarView.getHeight()*1/4)){
    			//canvas.drawRect(startX, startY, stopX, shakebarView.getHeight()- stopY, green);    			
    			pink_bar.setBounds(startX, (int) ((startY-pink_bar.getIntrinsicHeight())/mContext.getResources().getDisplayMetrics().density), startX+pink_bar.getIntrinsicWidth(),startY);
    			pink_bar.draw(canvas);
    			//dark_pink_bar = Tools.resizeImage(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.dark_pink_bar), stopX-startX,startY-stopY);
    			//dark_pink_bar.setBounds(startX, startY-dark_pink_bar.getIntrinsicHeight(), startX+dark_pink_bar.getIntrinsicWidth(),startY);
    			//dark_pink_bar.draw(canvas);
    			//canvas.drawRect(startX, startY-(startY-stopY), startX+background_bar.getIntrinsicWidth(), startY,green);
    			RectF rect = new RectF(startX, stopY, startX+background_bar.getIntrinsicWidth(), startY);
    			canvas.drawRoundRect(rect, 8, 8, green);
    			//Log.d("BAR", "startY: "+startY+" stopY: "+stopY);
    			background_bar.setBounds(startX, (int)((startY-background_bar.getIntrinsicHeight())/mContext.getResources().getDisplayMetrics().density), startX+background_bar.getIntrinsicWidth(), startY);
    			background_bar.draw(canvas);
    		}else{
    			Toast.makeText(getBaseContext(), "gotcha", Toast.LENGTH_SHORT).show();
    			stopY= startY;
    			
    		}
    			
    		
    		//Log.d(TAG,  "stopX: " + stopX +" stopY: " + stopY +" mOrientation[1]: "+ Math.abs(mAccelerometerValues[1]));
    		
    	}
    }
	
	/* (non-Javadoc)
	 * @see android.hardware.SensorEventListener#onAccuracyChanged(android.hardware.Sensor, int)
	 */
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see android.hardware.SensorEventListener#onSensorChanged(android.hardware.SensorEvent)
	 */
	@Override
	public void onSensorChanged(SensorEvent event) {
		//Log.d(TAG, "entra en sensorEvent");
           switch(event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                    mAccelerometerValues[0] = event.values[0];
                    mAccelerometerValues[1] = event.values[1];
                    mAccelerometerValues[2] = event.values[2];
                    //Log.d(TAG, "mOrientation tiene en onsensorchanged:" + mOrientationValues[0] + "-" + mOrientationValues[1]);
                    shakebarView.invalidate();
                break;
            }
	}

}
